// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UIGetText.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.UI.Forms
{
    #region Using Directives

    using System;
    using System.Windows.Forms;

    using Org.OpenTrader.Framework.Events;

    #endregion

    /// <summary>
    /// The ui get text.
    /// </summary>
    public partial class UIGetText : UIControl
    {
        #region Constants and Fields

        /// <summary>
        /// The handler.
        /// </summary>
        private EventHandler<TextEventArgs> handler;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UIGetText"/> class.
        /// </summary>
        public UIGetText()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIGetText"/> class.
        /// </summary>
        /// <param name="handler">
        /// The handler.
        /// </param>
        public UIGetText(EventHandler<TextEventArgs> handler)
        {
            this.InitializeComponent();
            this.handler = handler;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIGetText"/> class.
        /// </summary>
        /// <param name="text">
        /// The text.
        /// </param>
        /// <param name="handler">
        /// The handler.
        /// </param>
        public UIGetText(string text, EventHandler<TextEventArgs> handler)
        {
            this.InitializeComponent();
            this.handler = handler;
            this.textbox.Text = text;
        }

        #endregion

        #region Methods

        /// <summary>
        /// The textbox_ key down.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnTextBoxKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.handler(this, new TextEventArgs(this.textbox.Text));
                this.ContainingForm.Destroy();
            }
        }

        #endregion
    }
}